/*
 * Copyright 2008 The Microproperties project @sourceforge.net
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.microproperties.midp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.sf.microproperties.Properties;

/**
 * The <code>FileProperties</code> class is used for reading properties from a
 * file.
 * 
 * Note: This class requires <code>FileConnection</code> (JSR-75 support).
 * 
 * @author Johan Karlsson (johan.karlsson@jayway.se)
 * 
 * @since 0.1
 */
public class FileProperties extends Properties {
	
	private static final String FILE_PROTOCOL = "file:///";

	/**
	 * Load the properties from the specified filename.
	 * 
	 * @param fileName
	 *            the filename
	 */
	public void load(String fileName) {

		InputStream inputStream;

		try {
			String fileURL = FILE_PROTOCOL + fileName;
			inputStream = (InputStream) Connector.openInputStream(fileURL);
			load(inputStream);
		} catch (IOException e) {
			System.err.println("Could not open file " + fileName);
		}
	}

	/**
	 * Load the properties using the specified <code>FileConnection</code>.
	 * 
	 * @param fileConnection
	 */
	public void load(FileConnection fileConnection) {
		
		InputStream inputStream;
		
		try {
			inputStream = fileConnection.openInputStream();
			load(inputStream);
		} catch (IOException e) {
			System.err.println("Could not open InputStream for " + fileConnection.getName());
		}
	}
	
	/**
	 * Save the properties using the specified fileName.
	 * 
	 * @param fileName
	 */
	public void save(String fileName, String header){
		
		OutputStream outputStream;
		
		try {
			outputStream = (OutputStream)Connector.openOutputStream(fileName);
			save(outputStream, header);
		} catch (IOException e) {
			System.err.println("Could not open InputStream for " + fileName);
		}
	}
}
